package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.domain.Member;
import com.itheima.mapper.MemberMapper;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @PackageName: com.itheima.service.impl
 * @ClassName: MemberServiceImpl
 * @Author: Solomon
 * @Date: 2020/7/14 2:18
 * @Description:
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    //根据手机号判断当前用户是否为会员
    @Override
    public Member findByTelephone(String telephone) {
        return memberMapper.findByTelephone(telephone);
    }

    //保存会员信息
    @Override
    public void addMember(Member member) {
        String password = member.getPassword();
        if (password != null){
            //对密码进行处理
            String md5 = MD5Utils.md5(password);
            member.setPassword(md5);
        }
        memberMapper.addMember(member);
    }

    //根据月份查询会员数量
    @Override
    public List<Integer> findMemberCountByMonth(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";//2018.05.31
            Integer count = memberMapper.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }
}